package com.keeping.aiimagery.controller;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.keeping.aiimagery.service.OrdersServicesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.Map;
import java.util.UUID;

/**
 * @author:zfx-t
 * @version:1.0
 */
@RestController
public class AIcutoutController {
    @Autowired(required = false)
    private OrdersServicesService ordersServicesService;
    private String apiUrl = "https://api.remove.bg/v1.0/removebg";

    String Mokkerapi = "Bearer 8c874391-5b38-42d0-9c0b-faf7814c5e91";
    String url_background = "https://api.mokker.ai/v1/backgrounds";

    String url_replace_background = "https://api.mokker.ai/v1/replace-background";
    int numberOfImages = 1;


    @PostMapping("/aicutout")
    private String AicutoutController(@RequestBody Map data) {
        String imagebase64 = (String) data.get("image");
        String userAccount = (String) data.get("userAccount");
        HttpResponse httpResponse = HttpRequest.post(apiUrl)
                .header("X-Api-Key", "dE5JV8SL4vPVShvvGFVnMCgL")
                .form("image_file_b64", imagebase64)
                .execute();

        byte[] result = httpResponse.bodyBytes();
        String base64Encoded = Base64.encode(result);
        if (base64Encoded != null) {
            if(ordersServicesService.cut(userAccount, 1, "AIcutout"))
            {
                System.out.println("扣费成功");
                return base64Encoded;
            }
        }
        return null;

    }

    @GetMapping("/background")
    public String getBackground(){
        String body = HttpRequest
                .get(url_background)
                .header("Authorization",Mokkerapi)
                .header("accept","application/json")
                .execute()
                .body();
        return body;
    }
    @PostMapping("/replace-background")
    public String getNewImg(@RequestBody Map data){
        String uuid = java.util.UUID.randomUUID().toString();
        String userAccount =(String) data.get("userAccount");
        String backgroundId = (String) data.get("background_id");
        String image = (String) data.get("image");
        //先存储图片
        byte[] bytes = Base64.decode(image);
        String filePath = "image/"+userAccount + "/" + uuid + ".jpg";
        FileUtil.touch(filePath);

        File file = FileUtil.writeBytes(bytes,filePath);

        //向远程api发送请求
        String body = HttpRequest
                .post(url_replace_background)
                .header("Authorization",Mokkerapi)
                .header("accept","application/json")
                .header("Content-Type","multipart/form-data")
                .form("image", file)
                .form("background_id","92b2b5d5-c3e1-4115-8b62-21ac2a715347")
                .form("number_of_images","1")
                .execute()
                .body();

        JSONObject jsonObject = JSONUtil.parseObj(body);
        //获取其中的图片64编码
        String imagebase64 = (String) jsonObject.getJSONArray("images").getJSONObject(0).getJSONObject("image").get("data");
        byte[] bytes_ = Base64.decode(image);
        String filePath_ = "image/"+userAccount + "/cut/" + uuid + ".jpg";
        File file_ = FileUtil.writeBytes(bytes,filePath);
        return imagebase64;

    }
}
